// This file contains material supporting section 3.7 of the textbook:
//"Object Oriented Software Engineering" and is issued under the open-source
//license found at www.lloseng.com 

package client;
import ocsf.client.*;
import common.*;
import java.io.*;

/**
* This class overrides some of the methods defined in the abstract
* superclass in order to give more functionality to the client.
*
* @author Dr Timothy C. Lethbridge
* @author Dr Robert Lagani&egrave;
* @author Fran&ccedil;ois B&eacute;langer
* @version July 2000
*/
public class ChatClient extends AbstractClient
{
//Instance variables **********************************************

/**
* The interface type variable.  It allows the implementation of 
* the display method in the client.
*/
public ServerMsgLogIn sermsg;
public Student student_obj;
public Teacher teacher_obj;
public SecretaryANDPrincipal secretary_Principal_obj; 
public Parents parent_obj;
public String Massage;
public Course course_obj;
boolean	recived=false;
ChatIF clientUI; 
final public static int DEFAULT_PORT = 5555;	

//Constructors ****************************************************

/**
* Constructs an instance of the chat client.
*
* @param host The server to connect to.
* @param port The port number to connect on.
* @param clientUI The interface type variable.
*/

public ChatClient(String host, int port) 
 throws IOException 
{
	
 super(host, port); //Call the superclass constructor
 openConnection();
}


//Instance methods ************************************************
 

/**
* This method handles all data that comes in from the server.
*
* @param msg The message from the server.
*/
public void handleMessageFromServer(Object msg) 
{
	if(msg instanceof ServerMsgLogIn)
		sermsg = (ServerMsgLogIn)msg;
	
	if(msg instanceof Student)
		 student_obj = (Student)msg;
	
	if(msg instanceof SecretaryANDPrincipal)
		secretary_Principal_obj=(SecretaryANDPrincipal)msg;
	
	if(msg instanceof Parents)
		parent_obj=(Parents)msg;
	
	if(msg instanceof String)
		Massage=(String)msg;
	
	if(msg instanceof Course)
		course_obj=(Course)msg; 
	
	
	recived=true;
}

/**
* This method handles all data coming from the UI            
*
* @param message The message from the UI.    
*/
public void handleMessageFromClientUI(String message)
{
 try
 {
	 recived=false;
	 
 	sendToServer(message);
 	
 	while(recived==false);
 }
 catch(IOException e)
 {
   display
     ("Could not send message to server.  Terminating client.");
   quit();
 }
}



/**
* This method terminates the client.
*/
public void quit()
{
 try
 {
   closeConnection();
 }
 catch(IOException e) {}
 System.exit(0);
}

public void accept() 
{
 try
 {
   BufferedReader fromConsole = 
     new BufferedReader(new InputStreamReader(System.in));
   String message;

   while (true) 
   {
     message = fromConsole.readLine();
     handleMessageFromClientUI(message);
   }
 } 
 catch (Exception ex) 
 {
   System.out.println
     ("Unexpected error while reading from console!");
 }
}

/**
* This method overrides the method in the ChatIF interface.  It
* displays a message onto the screen.
*
* @param message The string to be displayed.
*/
public void display(String message) 
{
 System.out.println("> " + message);
}


}
//End of ChatClient class